package com.devgroupvn.dota.hero;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.devgroupvn.dota.Application;
import com.devgroupvn.dota.R;
import com.devgroupvn.dota.data.Hero;
import com.kin.bitmapcatching.DisplayBitmap.ObserveCatching;
import com.kin.bitmapcatching.ImageDownloader;
import com.kin.bitmapcatching.ImageDownloader.AsyncDownloadDrawable;
import com.kin.bitmapcatching.ImageDownloader.BitmapDownloaderTask;

public class AdapterHerro extends BaseAdapter implements ObserveCatching
{
	private List<Hero> mList;
	Context mContext;

	public AdapterHerro(List<Hero> _list, Context _context)
	{
		mList = _list;
		mContext = _context;
	}

	@Override
	public int getCount()
	{
		return mList.size();
	}

	@Override
	public Object getItem(int arg0)
	{
		return mList.get(arg0);
	}

	@Override
	public long getItemId(int arg0)
	{
		return arg0;
	}

	public static class ViewHolder
	{
		ImageView ivAvatar;
		TextView tvName;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
//		(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(mList==null)
		{
			return null;
		}
		Hero entry = mList.get(position);
		ViewHolder holder = new ViewHolder();
		if(convertView==null)
		{
			convertView = inflater.inflate(R.layout.herolist_layout, null);
			holder.ivAvatar = (ImageView)convertView.findViewById(R.id.iv_herolist);
			holder.tvName = (TextView)convertView.findViewById(R.id.tv_herolist);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvName.setText(entry.name);
		if(entry.image!=null && !"".equals(entry.image))
		{
			loadBitmapWithMemCache(entry.image, holder.ivAvatar);
		}
		return convertView;
	}

	@Override
	public void addBitmapToMemCache(String key, Bitmap bitmap)
	{
		if (getBitmapFromMemCache(key) == null)
		{
			if (bitmap != null)
			{
				Application.mMemCache.put(key, bitmap);
			}
		}

		if (!Application.mDiskCache.containsKey(key))
		{
			Application.mDiskCache.put(key, bitmap);
		}
	}

	@Override
	public Bitmap getBitmapFromMemCache(String key)
	{
		return Application.mMemCache.get(key);
	}

	@Override
	public Bitmap getBitmapFromDiskCache(String key)
	{
		return Application.mMemCache.get(key);
	}

	private void loadBitmapWithMemCache(String fileName, ImageView imageView)
	{
		if (ImageDownloader.cancelPotentialWork(fileName, imageView))
		{
			final String imageKey = String.valueOf(fileName);
			final Bitmap bitmap = getBitmapFromMemCache(imageKey);
			if (bitmap == null)
			{
				ImageDownloader imageDownloader = new ImageDownloader(mContext, this);
				final BitmapDownloaderTask task = imageDownloader.new BitmapDownloaderTask(imageView, mContext);
				Bitmap temp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon);
				final AsyncDownloadDrawable asyncDrawable = new AsyncDownloadDrawable(mContext.getResources(), temp,
						task);
				imageView.setImageDrawable(asyncDrawable);
				task.execute(fileName);
			}
			else
			{
				imageView.setImageBitmap(bitmap);
			}
		}
	}

}
